//
//  SEGMarketDetailViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGMarketDetailViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>

@implementation SEGMarketDetailViewModel
-(instancetype)init{
    if (self == [super init]) {
        _title = @"";
        _content = @"";
        _price = @"";
        //        _imageContent = @"";
        _isChange = @"";
    }
    return self;
}
@end

/**************************SEGMarketDetailViewLayout************************/
@implementation SEGMarketDetailViewLayout
- (instancetype)initWithData:(SEGMarketDetailViewModel *)data type:(SEGEnumMarketDetailViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segMarketDetailViewLayout];
    return self;
}

-(void)segMarketDetailViewLayout{
    //高度固定
    _totalHeight = kMarketDetailViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _titleLabelHeight = 0;
    _titleLabelTextLayout = nil;
    _contentLabelHeight = 0;
    _contentLabelTextLayout = nil;
    _priceLabelHeight = 0;
    _priceLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    
    [self segpriceLabelWithContent:_data.price fontSize:15 textColor:kColor_S1];
    [self segtitleLabelWithContent:_data.title fontSize:15 textColor:kColor_1];
    [self segcontentLabelWithContent:_data.content fontSize:15 textColor:kColor_1];
    
    [self segImages];
    
    _totalHeight = _titleLabelHeight + _contentLabelHeight + labelPadding + picTopPadding + _imageContentViewHeight;
}

-(void)segImages{
    CGFloat imageHeight = picWidth * kScaleWidth;
    CGFloat padding = picPadding;
    CGFloat imageTotalHeight = 0;
    
    NSUInteger imageCount = _data.imageContent.count;
    
    if (imageCount== 1) {
        imageTotalHeight = 170 * kScaleWidth;
    }
    
    if (imageCount> 1 && imageCount <= 3) {
        imageTotalHeight = imageHeight;
    }
    if (imageCount > 3 && imageCount <= 6) {
        imageTotalHeight = imageHeight * 2 + padding;
    }
    if (imageCount > 6 && imageCount <= 9) {
        imageTotalHeight = imageHeight * 3 + padding * 2;
    }
    self.imageContentViewHeight = imageTotalHeight;
}

//控件titleLabel文本布局计算
-(void)segtitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - _priceLabelWidth - 5, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    container.truncationType = SEGTextTruncationTypeEnd;
    
    _titleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_titleLabelTextLayout)return;
    _titleLabelHeight = [modifier heightForLineCount:_titleLabelTextLayout.rowCount];
}

//控件contentLabel文本布局计算
-(void)segcontentLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    if ([_data.isChange isEqualToString:@"1"]) {//可交换
        NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_exchange")] FontSize:fontSize contentInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        if (img) {
            [text insertAttributedString:img atIndex:0];
        }
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    
    _contentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentLabelTextLayout)return;
    _contentLabelHeight = [modifier heightForLineCount:_contentLabelTextLayout.rowCount];
    
    
    
}

//控件priceLabel文本布局计算
-(void)segpriceLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    
    CGFloat w = [SEGKitHelper seg_sizeWithString:content fontSize:fontSize textColor:color].width;
    //    CGRect rect = [content boundingRectWithSize:CGSizeMake(_data.widgetWidth, fontSize + 2)
    //                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
    //                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
    //                                       context:nil];
    //    CGSize size = [content sizeWithAttributes:@{NSFontAttributeName:font}];
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    
    _priceLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_priceLabelTextLayout)return;
    _priceLabelHeight = [modifier heightForLineCount:_priceLabelTextLayout.rowCount];
    _priceLabelWidth = w;
}

@end
